package com.journey.org.ui.knowledge.play

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import com.google.android.material.tabs.TabLayout
import com.journey.org.BR
import com.journey.org.R
import com.journey.org.databinding.FragmentPlayAndroidMenuBinding
import com.journey.org.ui.knowledge.gankio.list.GankListFragment
import com.journey.org.ui.knowledge.play.list.PlayAndroidListFragment
import com.mvvmhabit.org.base.BaseFragment
import com.mvvmhabit.org.base.BaseFragmentPagerAdapter

/**
 * @Date: 2020/12/18 10:20
 * @Author:小妖
 * @Description: 玩Android 目录
 */
class PlayAndroidMenuFragment :
    BaseFragment<FragmentPlayAndroidMenuBinding, PlayAndroidMenuViewModel>() {
    private val mFragment = ArrayList<Fragment>()

    private val mTitle = ArrayList<String>()

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): Class<PlayAndroidMenuViewModel> =
        PlayAndroidMenuViewModel::class.java

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.fragment_play_android_menu

    override fun initData() {
        super.initData()
        mViewModel?.onPlayAndroidMenuDataEvent?.observe(this, Observer {
            it.forEach { entity ->
                mTitle.add(entity.name)
                val fragment = PlayAndroidListFragment()
                val bundle = Bundle()
                bundle.putInt("id", entity.id)
                fragment.arguments = bundle
                mFragment.add(fragment)
            }
            initAdapter()
        })
    }


    private fun initAdapter() {
        mBinding.layoutMenuViewpager.offscreenPageLimit = 1
        val adapter = BaseFragmentPagerAdapter(activity!!.supportFragmentManager, mFragment, mTitle)
        mBinding.layoutMenuViewpager.adapter = adapter
        mBinding.layoutMenuTab.setupWithViewPager(mBinding.layoutMenuViewpager)
        mBinding.layoutMenuViewpager.addOnPageChangeListener(
            TabLayout.TabLayoutOnPageChangeListener(
                mBinding.layoutMenuTab
            )
        )
    }
}